package a02_链表;

/**
 * <p>
 * a03_反转链表
 * 注意：prev一定先设置为null，要不然2 -> 1之后，1 -> 2的一直不断开
 * </p>
 *
 * @author flyduck
 * @since 2024-06-18
 */
public class a04_反转链表复习4 {

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(4);
        head.next.next.next.next = new ListNode(5);
        ListNode listNode = reverseList(head);
        ListNode temp = listNode;
        while (temp != null){
            System.out.println(temp.val);
            temp = temp.next;
        }
    }

    public static ListNode reverseList(ListNode head) {
        ListNode prevNode = null;
        ListNode currentNode = head;
        while (currentNode != null) {
            ListNode temp = currentNode.next;

            currentNode.next = prevNode;
            prevNode = currentNode;
            currentNode = temp;
        }
        return prevNode;
    }
}
